#!/bin/sh

. $IPKG_INSTROOT/lib/functions.sh
. $IPKG_INSTROOT/lib/functions/service.sh

gen_user_iptables() {
	config_get enable $1 enable
	[ "$enable" = "0" ] && return 0
	config_get remarks $1 remarks
	config_get port $1 port
	iptables -A V2RAY-SERVER -p tcp --dport $port -m comment --comment "$remarks" -j ACCEPT
	iptables -A V2RAY-SERVER -p udp --dport $port -m comment --comment "$remarks" -j ACCEPT
}

iptables -F V2RAY-SERVER 2>/dev/null
iptables -D INPUT -j V2RAY-SERVER 2>/dev/null
iptables -X V2RAY-SERVER 2>/dev/null

enable=$(uci get v2ray_server.@global[0].enable)
if [ $enable -eq 1 ]; then
	iptables -N V2RAY-SERVER
	iptables -I INPUT -j V2RAY-SERVER
	config_load v2ray_server
	config_foreach gen_user_iptables "user"
fi